package com.gitee.cui.netty.bytebuf;

import com.gitee.cui.util.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.Test;

/**
 * 缓冲区池引用测试类
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/22 13:01
 **/
public class ReferenceTest {

    @Test
    public void referenceTest() {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        Logger.info("after create:" + buffer.refCnt());
        buffer.retain();
        Logger.info("after retain:" + buffer.refCnt());
        buffer.release();
        Logger.info("after release:" + buffer.refCnt());
        buffer.release();
        Logger.info("after release:" + buffer.refCnt());
        // 此处会抛出异常，因为缓冲区在引用次数为0时，不能再被使用
        // io.netty.util.IllegalReferenceCountException: refCnt: 0, increment: 1
        buffer.retain();
        Logger.info("after retain:" + buffer.refCnt());
    }
}
